﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace EM.OA.WebCommon
{
    /// <summary>
    /// 验证登录权限
    /// </summary>
    public class AppAuthorizeAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            // 过滤不需要验证的页面   allowGuestPages.Add("controllerName", "|actionName1|actionName2|");
            // 过滤整个控制器用 allowGuestPages.Add("controllerName", "all");
            Dictionary<string, string> allowGuestPages = new Dictionary<string, string>();
            allowGuestPages.Add("login", "all");

            string currentControllerName = filterContext.RouteData.Values["Controller"].ToString().ToLower();// MvcUtil.GetControllerName(filterContext.RouteData).ToLower();
            string currentActionName = filterContext.RouteData.Values["Action"].ToString().ToLower();// MvcUtil.GetActionName(filterContext.RouteData).ToLower();

            string allowActions = string.Empty;
            if (allowGuestPages.TryGetValue(currentControllerName, out allowActions))
            {
                allowActions = allowActions.ToLower();
                if (allowActions == "all" || allowActions.Contains("|" + currentActionName + "|"))
                {
                    return;
                }
            }

            // 没有登录，跳到登录页面
            if (HttpContext.Current.Session["UserID"] == null)
            {
                filterContext.Result = new RedirectResult("/Login");// RedirectToRouteResult("login", routeValues);
            }

        }

    }
}
